import sys
file = sys.stdout
file.write("hello")

file1 = open('E:\\a.txt')		# 以只读方式打开E盘的文本文件a.txt
file2 = open('b.txt', 'w')		# 以只写方式打开当前目录的文本文件b.txt
file3 = open('c.txt', 'w+')		# 以读/写方式打开文本文件c.txt
file4 = open('d.txt', 'wb+')	# 以读/写方式打开二进制文件d.txt

file.close()

with open('a.txt') as f:
    pass

with open('file.txt', mode='r') as f:
    print(f.read(2))   						# 读取两个字节的数据
    print(f.read())    						# 读取剩余的全部数据

with open('file.txt', mode='r', encoding='utf-8') as f:
    print(f.readline())
    print(f.readline())

with open('file.txt', mode='r', encoding='utf-8') as f:
    print(f.readlines())                                                                 # 使用readlines()方法读取数据

string = "Here we are all, by day; by night."		# 字符串
with open('write_file.txt', mode='w', encoding='utf-8') as f:
    size = f.write(string)					# 写入字符串
    print(size)						# 打印字节数

with open('file.txt') as f:
    print(f.tell())					# 获取文件读写位置
    print(f.read(5))				# 利用read()方法移动文件读写位置
    print(f.tell())					# 再次获取文件读写位置


